--
-- Author: shunguo.chen
-- Date: 2018-02-06 10:57:46
--
-- WorldMapController 世界地图

local WorldMapView = import("..views.WorldMapView")
local MapView = import("..views.MapView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(params)
	self.super.ctor(self, params)
	
end

function ClassRef:init()
	self._model = app:getInst("WorldMapModel")

end

function ClassRef:removeListeners()
	self.super.removeListeners(self)
end

-- WorldMapView
function ClassRef:openView(data)
	if cc.isDead(self._view) then
		self._view = WorldMapView.new(app.mvc.VIEWTYPE_BLUR)
		self._view:onCloseView(handler(self, self.closeView))
		self._view:onClearView(handler(self, self.clearView))
		self._view:onOperate(handler(self, self.onOperate))
	end
	self:addChild(self._view, data)
	self._view:show()
	local maps = self._model:getMapInfos()
	self._view:refresh(maps)

    app:sendMsg("TeamModel", "reqPostPlayerState", Constant.FUNC_TYPE_BIGMAP)
end
function ClassRef:clearView()
	if self._view then 
		self._view = nil
		-- self:closeMapView()
	end
end

-- MapView
-- data = {
-- 	mapId = 201, -- 地图id
-- 	isFromWorld = true/false, -- 是否从世界地图打开
-- }
function ClassRef:openMapView(data)
	data = data or {mapId=MapManager:getMapId()}
	local mapInfo = self._model:getMapInfo(data.mapId)
	if not mapInfo or not mapInfo:hasZone() then 
		self:openView()
		return
	end

	if nil == self._mapView then
		self._mapView = MapView.new(app.mvc.VIEWTYPE_BLUR)
		self._mapView:onCloseView(handler(self, self.closeMapView))
		self._mapView:onClearView(handler(self, self.clearMapView))
		self._mapView:onOperate(handler(self, self.onOperate))
	else
		-- self._mapView:onClearView(nil)
		-- self._mapView:retain()
		-- self:removeChild(self._mapView, data)
		-- self:addChild(self._mapView, data)
		-- self._mapView:onClearView(handler(self, self.clearMapView))
		-- self._mapView:release()
	end
	self:addChild(self._mapView, data)

	self._mapView:setVisible(false)
	self._model:reqWorldBlessInfo(function()
		if cc.isDead(self._mapView) then return end
		self._mapView:show()

		local isFromWorld = data.isFromWorld or false
		self._mapView:refresh(mapInfo, isFromWorld)
	end)

	app:sendMsg("TeamModel", "reqPostPlayerState", Constant.FUNC_TYPE_MAP)
end
function ClassRef:closeMapView(data)
	if self._mapView then
		self:removeChild(self._mapView, data)
		self:clearMapView()
	end
end
function ClassRef:clearMapView()
	if self._mapView then 
		self._mapView = nil
	end
end

---------------------------------------------------------------


------------------------------------------------------------
function ClassRef:onOperate(op, data)
	-- print("WorldMapController:onOperate op = " .. op)
	if op == "openWorld" then
		self:closeMapView()
		self:openView()
	elseif op == "gotoZone" then
		-- data = {mapId=, pos=}
		EM:notify("onTrackingWay",{trackNpcId={mapId=data.mapId, x=data.pos}, autoStartEvents=false, isTracking=true,immediately = true})
		self:closeView()
	elseif op == "gotoNpc" then
		-- data = {mapId=, npcId=}
		EM:notify("onTrackingWay",{trackNpcId=data.npcId, autoStartEvents=false, isTracking=true,immediately = true})
		self:closeView()
	elseif op == "goHome" then
		-- 回自己飞船
		if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_HOME) then
			app:getLaunchCtl():endCharacterMove()
			MapManager:gotoMap({mapId=301})
			self:closeView()
		else
			local msg = UIHandler:getFunctionOpenCondition(Constant.FUNC_TYPE_HOME)
			display.pushToast(msg)
		end
	elseif op == "openMap" then
		-- data = mapId 从世界地图打开地图 
		local mapInfo = self._model:getMapInfo(data)
		if mapInfo then
			if mapInfo:hasZone() then
				local param = {
					mapId = data,
					isFromWorld = true
				}
				self:openMapView(param)
			else
				-- TODO csg 没有分区域的直接打开前往地图
				app:getLaunchCtl():endCharacterMove()
				MapManager:gotoMap({mapId=data})
				self:closeView()
			end
		end
	end
end

return ClassRef